Ext.define('USocial.controller.LocationUpdate',{
	extend : 'Ext.app.Controller',
	
	init : function(){
		var friendsStore = Ext.getStore('FriendsStore');
			appUserStore = Ext.getStore('AppUserStore');
			
		var data = {}
		data.friends = [
			{ uid : 1307600, lat : 53.9, lng : 27.5},
			{ uid : 1413833, lat : 53.89, lng : 27.55},
			{ uid : 1780097, lat : 53.79, lng : 27.45},
			{ uid : 1998071, lat : 53.69, lng : 27.5},
			{ uid : 2360770, lat : 53.79, lng : 27.55}
		];
		
		friendsStore.on('load', function(store, records, successful, operation){
			//ajax request .. on success : 
			data.friends.forEach(function(friend){
				store.getById(friend.uid).setPosition(friend.lat, friend.lng);
			});
		});
		
		appUserStore.on('load', function(store, records, successful, operation){
			var appUser = USocial.Util.getAppUser();
			
			var geo = Ext.create('Ext.util.Geolocation', {
				frequency : 5000,
				autoUpdate: true,
				allowHighAccuracy: true,
				listeners: {
					locationupdate: function(geo){
						var lat = geo.getLatitude(),
							lng = geo.getLongitude();
						
						if(appUser.get('lat') !== lat && appUser.get('lng')!== lng){
							appUser.setPosition(lat, lng);
							//WebApi.updateLocation
						}
					},
					locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message){
						var builtMessage = 'bTimeout:'+bTimeout+' bPermissionDenied:'+bPermissionDenied+' locationUnavailable:'+bLocationUnavailable+' message:'+message;
						if(Ext.device && Ext.device.Notification){
							Ext.device.Notification.show({
							    title: 'Failure',
							    message: builtMessage,
							    buttons: Ext.MessageBox.OK
							});
						}else{
							alert(builtMessage);
						}
					}
				}
    		});
		});
	}
});